﻿using MyFurion.Application;

namespace MyFurion.Start
{
    /// <summary>
    /// 数据库记录日志
    /// </summary>
    public class DatabaseLoggingWriter : IDatabaseLoggingWriter
    {
        // 支持构造函数注入任何实例，会自动释放任何服务，比如注入 IRepository，或者 SqlSugarClient
        private readonly SqlLogRepository _logRepository;
        public DatabaseLoggingWriter(SqlLogRepository logRepository)
        {
            _logRepository= logRepository;
        }
        public void Write(LogMessage logMsg, bool flush)
        {
            // 这里写你任何插入数据库的操作，无需 try catch
            var result = _logRepository.Add(new SqlLog()
            {
                LogLevel = logMsg.LogLevel.ToString(),
                LogName = logMsg.LogName,
                Remark = logMsg.Message
            }).Result;
        }
    }
}
